﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using WinFormsAppTest.Dao;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsAppTest
{
    public partial class UserAddForm : Form
    {
        //
        public String username = "null";//设置默认登录用户(手动添加)
        public int id = 0;//设置默认登录用户(手动添加)
        //
        public UserAddForm()
        {
            InitializeComponent();
        }

        // -------有参 窗口（手动添加）--------
        public UserAddForm(String name, int uid)
        {
            username = name;
            id = uid;
            InitializeComponent();
        }
        //-----------------------------------

        private void OKbutton_Click(object sender, EventArgs e)
        {
            TestDao registerdao = new TestDao();
            AdminDao admindao = new AdminDao();
            WorkDao workdao = new WorkDao();
            if (string.IsNullOrWhiteSpace(UserNametextBox.Text))
            {
                MessageBox.Show("添加账号不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(PasswordtextBox.Text))
            {
                MessageBox.Show("添加密码不能为空！");
            }
            else if (admindao.GetUserByPassword(PasswordtextBox.Text).Count != 0)
            {
                MessageBox.Show("注册密码已使用！");
            }
            else if (admindao.GetUserByName(UserNametextBox.Text).Count != 0)
            {
                MessageBox.Show("用户名已使用！");
            }
            else
            {
                registerdao.CreateUser(UserNametextBox.Text, PasswordtextBox.Text);
                workdao.CreateWork(admindao.GetUserByName(UserNametextBox.Text)[0].Userid);
                MessageBox.Show("注册成功！");
                this.Hide();
                Form1 f = new Form1();
                f.ShowDialog();
                this.Dispose();
                return;
            }
        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            UserForm f2 = new UserForm(username, id);
            this.Hide();
            f2.ShowDialog();
            this.Dispose();
        }

        private void UserAddForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }
    }
}
